//
//  PPPetLoveFiteTableViewController.m
//  pet
//
//  Created by 陈无忌 on 12/22/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPPetLoveFiteTableViewController.h"
#import "supportingHeadFiles.h"
#import "PPSystemConfiguraton.h"

@interface PPPetLoveFiteTableViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>

@property (strong, nonatomic) NSArray *petTypeArray;
@property (strong, nonatomic) NSArray *petBreedArray;

@property (weak, nonatomic) IBOutlet UITextField *typeTF;
@property (weak, nonatomic) IBOutlet UITextField *breedTF;
@property (weak, nonatomic) IBOutlet UIButton *petMale;
@property (weak, nonatomic) IBOutlet UIButton *petFemale;
@property (weak, nonatomic) IBOutlet UIButton *masterMale;
@property (weak, nonatomic) IBOutlet UIButton *masterFemale;
@property (weak, nonatomic) IBOutlet UISegmentedControl *cityChoseSC;

@property (nonatomic, strong) UIPickerView *pickerView;
@property (strong, nonatomic) UIToolbar *toolBar;
@property (strong, nonatomic) UIBarButtonItem *cancelButton;
@property (strong, nonatomic) UIBarButtonItem *doneButton;

@property (nonatomic) int petSex;
@property (nonatomic) int masterGender;

@property (nonatomic, weak) PPSystemConfiguraton *config;

@end

@implementation PPPetLoveFiteTableViewController
- (PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}


//pickerView
-(UIPickerView *)pickerView
{
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 260)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}
-(UIToolbar *)toolBar
{
    if (!_toolBar) {
        _toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
        UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        NSArray *toolBarItems = [[NSArray alloc] initWithObjects:self.cancelButton, space, self.doneButton, nil];
        [_toolBar setItems:toolBarItems];
    }
    return _toolBar;
}
- (UIBarButtonItem *)cancelButton
{
    if (!_cancelButton) {
        _cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
    }
    return _cancelButton;
}
- (UIBarButtonItem *)doneButton
{
    if (!_doneButton) {
        _doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
    }
    return _doneButton;
}

- (void)cancelButtonPressed:(UIBarButtonItem *)sender
{
    if ([self.typeTF isFirstResponder]) {
        [self.typeTF resignFirstResponder];
    }else{
        [self.breedTF resignFirstResponder];
    }
    
}
- (void)doneButtonPressed:(UIBarButtonItem *)sender
{
    if ([self.typeTF isFirstResponder]) {
        [self.typeTF resignFirstResponder];
        if (![self.typeTF.text isEqualToString:[self.petTypeArray objectAtIndex:[self.pickerView selectedRowInComponent:0]]]) {
            self.breedTF.text = @"全部";
        }
        self.typeTF.text = [self.petTypeArray objectAtIndex:[self.pickerView selectedRowInComponent:0]];
        self.petBreedArray = [PPPetTypeArrayList getBreedWithType:self.typeTF.text];
        
    }else{
        [self.breedTF resignFirstResponder];
        self.breedTF.text = [self.petBreedArray objectAtIndex:[self.pickerView selectedRowInComponent:0]];
    }
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //default pet sex
    self.petMale.selected = YES;
    self.petSex = 0;
    //default master gender
    self.masterMale.selected = YES;
    self.masterFemale.selected = YES;
//    self.masterGender = 0;
    //
    self.cityChoseSC.layer.cornerRadius = 8;
    self.cityChoseSC.layer.masksToBounds = YES;
    
    self.typeTF.delegate = self;
    self.breedTF.delegate = self;
    
    self.typeTF.inputView = self.pickerView;
    self.typeTF.inputAccessoryView = self.toolBar;
    self.breedTF.inputView = self.pickerView;
    self.breedTF.inputAccessoryView = self.toolBar;
    
    self.petTypeArray = [PPPetTypeArrayList getType];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -Custom IBAction 

- (IBAction)petMale:(UIButton *)sender {
    self.petMale.selected = YES;
    self.petFemale.selected = NO;
    self.petSex = 0;
    
    
}

- (IBAction)petFemale:(UIButton *)sender {
    self.petMale.selected = NO;
    self.petFemale.selected = YES;
    self.petSex = 1;
}

- (IBAction)masterMale:(UIButton *)sender {
    if (sender.selected) {
        sender.selected = NO;
    }else{
        sender.selected = YES;
    }
//    self.masterMale.selected = YES;
//    self.masterFemale.selected = NO;
//    self.masterGender = 0;
}

- (IBAction)masterFemale:(UIButton *)sender {
    if (sender.selected) {
        sender.selected = NO;
    }else{
        sender.selected = YES;
    }
//    self.masterMale.selected = NO;
//    self.masterFemale.selected = YES;
//    self.masterGender = 1;
}

- (IBAction)completeButton:(id)sender {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if(![self.typeTF.text isEqualToString:@"全部"]){
        [params setObject:self.typeTF.text forKey:@"type"];
    }
    if(![self.breedTF.text isEqualToString:@"全部"]){
        [params setObject:self.breedTF.text forKey:@"breed"];
    }
    if(self.cityChoseSC.selectedSegmentIndex == 1){
        [params setObject:self.config.userInfo.address forKey:@"city"];
    }
    if (self.masterMale.selected == YES && self.masterFemale.selected == NO) {
        self.masterGender = 0;
        [params setObject:@(self.masterGender) forKey:@"masterGender"];
    }else if (self.masterMale.selected == NO && self.masterFemale.selected == YES){
        self.masterGender = 1;
        [params setObject:@(self.masterGender) forKey:@"masterGender"];
    }else if (self.masterMale.selected == NO && self.masterFemale.selected == NO){
        [PPHUD hudShowWithString:@"请选择主人性别" andView:self.view];
        return;
    }
    [params setObject:@(self.petSex) forKey:@"petSex"];
//    [params setObject:@(self.masterGender) forKey:@"masterGender"];
    [params setObject:@(self.config.userInfo.id) forKey:@"userId"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"filtPetLove" object:nil userInfo:params];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)cityChose:(UISegmentedControl *)sender {
    if (sender.selectedSegmentIndex == 1) {
        if (self.config.userInfo.address.length == 0 || [self.config.userInfo.address isKindOfClass:[NSNull class]]) {
            [PPHUD hudShowWithString:@"请先去个人资料设置自己所在的城市" andView:self.view];
            sender.selectedSegmentIndex = 1;
        }
    }
}

#pragma mark - Pick view data soure
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView//返回选择器组件数
{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component//选择器的选项个数
{
    if ([self.typeTF isFirstResponder]) {
        return self.petTypeArray.count;
    }else{
        return self.petBreedArray.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if ([self.typeTF isFirstResponder]) {
        return [self.petTypeArray objectAtIndex:row];
    }else{
        return [self.petBreedArray objectAtIndex:row];
    }
}

#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [self.typeTF becomeFirstResponder];
    }else if (indexPath.row == 1){
        [self.breedTF becomeFirstResponder];
    }
}

#pragma mark - Table view data source


#pragma mark - UITextFiled delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([textField isEqual:self.typeTF]) {
        [self.pickerView reloadAllComponents];
        
    }else if ([textField isEqual:self.breedTF]){
        if ([self.typeTF.text isEqualToString:@"全部"]) {
            [PPHUD hudShowWithString:@"请先设置宠物类型" andView:self.view];
            return NO;
        }
        [self.pickerView reloadAllComponents];
    }
    return YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
